import vue from 'vue';
import vuex from 'vuex';
import * as api from '../api/api';

vue.use(vuex);
const state = {
    isLoading: false,
    token: localStorage.getItem('token'),
    // 新闻
    movies: null,
    mId: localStorage.getItem('mId'),
    mDetail: null,
    jokes: null,
    vedio: null,
    picture: null,
    weather: null,
    billboard: null,
    songList: null
}

const mutations = {
    // 登录状态
    LOGINSTATUS(state, payload) {
        state.token = payload.token;
    },
    // loading
    ISLOADING(state, payload) {
        state.isLoading = payload.isLoading;
    },
    // 电影
    GET_MOVIES(state, payload) {
        state.movies = payload.movies;
    },
    // 电影详情
    GET_BYMOVIESID(state, payload) {
        state.mDetail = payload.mDetail;
    },
    //存儲電影id
    SET_MOVIESID(state, payload) {
        state.mId = payload.mId;
    },
    GET_JOKES(state, payload) {
        state.jokes = payload.jokes;
    },
    GET_PICTURE(state, payload) {
        state.picture = payload.picture;
    },
    GET_WEATHER(state, payload) {
        if (payload.weather) {
            let { city, aqi, ganmao, wendu } = payload.weather;
            let { date, high, fx, low, fl, type, cls } = payload.weather.yesterday;
            date = date.split('日')[1];
            high = high.split(' ')[1];
            low = low.split(' ')[1];
            let yesterday = { date, high, fx, low, fl, type, cls };
            let forecast = payload.weather.forecast.map(item => {
                return {
                    date: item.date.split('日')[1],
                    high: item.high.split(' ')[1],
                    fengli: item.fengli,
                    low: item.low.split(' ')[1],
                    fengxiang: item.fengxiang,
                    type: item.type,
                    cls: item.cls
                }
            });
            state.weather = { city, aqi, ganmao, wendu, yesterday, forecast };
        } else {
            state.weather = null;
        }
    },
    GET_BILLBOARD(state, payload) {
        state.billboard = payload.billboard;
    },
    GET_MUSIC(state, payload) {
        state.songList = payload.songList;
    }
}

const actions = {
    GET_MOVIES({ commit }, params) {
        api.commonAPI('/movie/' + params.type, '')
            .then(res => {
                if (res.status == 200) {
                    commit('GET_MOVIES', { movies: res.data.subjects })
                }
            });
    },
    GET_BYMOVIESID({ commit }, params) {
        api.commonAPI('/movie/subject/' + params.mId, '')
            .then(res => {
                if (res.status == 200) {
                    commit('GET_BYMOVIESID', { mDetail: res.data });
                }
            });
    },
    //百科推荐
    GET_JOKES({ commit }, params) {
        let pd = 'type=' + params.type + "&page=1";
        api.commonAPIOPEN('/satinGodApi', pd)
            .then(res => {
                if (res.data.code == 200) {
                    commit('GET_JOKES', { jokes: res.data.data });
                }
            })
    },
    // 百科美图
    GET_PICTURE({ commit }, params) {
        let pd = 'page=' + params.page;
        api.commonAPIOPEN('/meituApi', pd)
            .then(res => {
                if (res.data.code == 200) {
                    commit('GET_PICTURE', { picture: res.data.data });
                }
            })
    },
    //百科天氣
    GET_WEATHER({ commit }, params) {
        let pd = 'city=' + params.city;
        api.commonAPIOPEN('/weatherApi', pd)
            .then(res => {
                if (res.data.code == 200) {
                    let { yesterday, forecast, city, aqi, ganmao, wendu } = res.data.data;
                    const p1 = actions.GET_WEATHERTYPE({ type: yesterday.type });
                    const p = forecast.map(item => {
                        return actions.GET_WEATHERTYPE({ type: item.type });
                    });
                    p.push(p1);
                    Promise.all(p).then(res => {
                        let len = res.length;
                        res.map((item, i) => {
                            if (i == len - 1) {
                                yesterday.cls = item;
                                let weather = { yesterday, forecast, city, aqi, ganmao, wendu };
                                commit('GET_WEATHER', { weather: weather });
                            } else {
                                forecast[i].cls = item;
                            }
                        })
                    })
                } else {
                    commit('GET_WEATHER', { weather: res.data.data });
                }
            })
    },
    GET_WEATHERTYPE(params) {
        return api.commonAPIJSON('/weather.json')
            .then(res => {
                if (res.status == 200) {
                    return res.data[params.type];
                }
            });
    },
    // 音乐
    GET_BILLBOARD({ commit }, params) {
        api.commonAPIJSON('/billboard.json')
            .then(res => {
                commit('GET_BILLBOARD', { billboard: res.data.billboard });
            })
    },
    GET_MUSIC({ commit }, params) {
        api.commonAPIJSON('/songlist.json')
            .then(res => {
                commit('GET_MUSIC', { songList: res.data.song_list });
            })
    }
}

export default new vuex.Store({
    state,
    actions,
    mutations
});